package com.servlet.yanzheng;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Picture extends HttpServlet{
   public void doGet(HttpServletRequest hsrq,HttpServletResponse hsrp){
	   HttpSession session=hsrq.getSession(true);
	   
	   hsrp.setContentType("image/jpeg");
	   try {
		OutputStream out=hsrp.getOutputStream();
		int width=80; 
		int height=20;
		BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		
		Graphics g=image.getGraphics();
		g.fillRect(0,0,width,height);
		Font f=new Font("Time New Roman",Font.ITALIC,18);
		g.setFont(f);
		
		Random r=new Random();
		String rand="";
		
		for(int i=0;i<4;i++){
			String s=String.valueOf(r.nextInt(10));
			rand=rand+s;
			
			g.setColor(Color.red);
			g.drawString(s,20*i+6,16);
			
		}
		    g.dispose();
		    ImageIO.write(image,"JPEG", out);
		    session.setAttribute("check",rand);
	} catch (Exception e) {
		e.printStackTrace();
	}
   }
}
